package com.muqi.app.qlearn.fragments;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageButton;

import com.muqi.app.network.http.MyAsyncHttp;
import com.muqi.app.network.http.NetWorkUtils;
import com.muqi.app.network.http.ParamsUtils;
import com.muqi.app.network.http.ResponseUtils;
import com.muqi.app.network.http.MyAsyncHttp.AsyncCallBackListener;
import com.muqi.app.network.http.MyAsyncHttp.RESULTCODE;
import com.muqi.app.project.contants.NetWorkApi;
import com.muqi.app.project.utils.ShowToast;
import com.muqi.app.qlearn.modles.TreeHomeBean;
import com.muqi.app.qlearn.student.BaseFragment;
import com.muqi.app.qlearn.student.MenuActivity;
import com.muqi.app.qlearn.student.R;
import com.muqi.app.user.widget.CubeOutTransformer;
import com.muqi.app.user.widget.ViewPagerScroller;

public class TabGrowTree extends BaseFragment implements OnClickListener {

	private ViewPager viewPager;
	private ImageButton changeButton;
	private boolean isFirst = true;
	private List<Fragment> listDatas;
	private int oldTreeLeaf = -1;
	private TabGrow grow;

	@Override
	@Nullable
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		View fileView = inflater.inflate(R.layout.fragment_grow_tree, null);

		return fileView;
	}

	@Override
	public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		loadingGrowTreeDatas();
		initView(view);

	}

	private void initView(View view) {
		viewPager = (ViewPager) view.findViewById(R.id.fragment_tree_vp);
		changeButton = (ImageButton) view.findViewById(R.id.changeImageButton);
		changeButton.setOnClickListener(this);

		listDatas = new ArrayList<Fragment>();
		 grow = new TabGrow();

		Bundle bundle = new Bundle();
		bundle.putInt("oldTreeLeaf", oldTreeLeaf);
		grow.setArguments(bundle);
		listDatas.add(grow);

		listDatas.add(new TabFileTree());
		// 设置viewpager的切换动画
		viewPager.setPageTransformer(true, new CubeOutTransformer());
		// 设置viewpager的切换时间
		ViewPagerScroller scroller = new ViewPagerScroller(mActivity);
		scroller.setScrollDuration(2000);
		scroller.initViewPagerScroll(viewPager);// 这个是设置切换过渡时间为2秒
		viewPager.setAdapter(new TreeFragmentPageAdapter(
				getChildFragmentManager()));
		viewPager.addOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int arg0) {
				if (arg0 == 0) {
					isFirst = true;
					changeButton.setImageResource(R.drawable.tree_traslation);
				} else {
					isFirst = false;
					changeButton.setImageResource(R.drawable.files_tree_button);
				}

			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
				// TODO Auto-generated method stub

			}
		});

	}

	private class TreeFragmentPageAdapter extends FragmentPagerAdapter {

		public TreeFragmentPageAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int arg0) {
			// TODO Auto-generated method stub
			return listDatas.get(arg0);
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return listDatas.size();
		}

	}

	@Override
	public void onClick(View arg0) {
		if (isFirst) {
			viewPager.setCurrentItem(1);
		} else {
			viewPager.setCurrentItem(0);
		}

	}

	private void loadingGrowTreeDatas() {
		if (NetWorkUtils.isNetworkAvailable(mActivity)) {
			HashMap<String, Object> sendMap = new HashMap<String, Object>();
			sendMap.put("token", mSpUtil.getToken());
			sendMap.put("class_manager_id", mSpUtil.getChildClassId());
			sendMap.put("grade_id", mSpUtil.getChildGradeId());
			sendMap.put("school_id", mSpUtil.getChildSchoolId());
			sendMap.put("student_id", mSpUtil.getCurrentChildId());
			String requestUrl = ParamsUtils.buildParams(
					NetWorkApi.GET_GROW_TREE_DATAS, sendMap);
			MyAsyncHttp.get(mActivity, requestUrl, new AsyncCallBackListener() {

				@Override
				public void onAsyncCallBack(RESULTCODE resuleCode,
						String request, String result) {
					if (resuleCode == RESULTCODE.SUCCESS) {
						TreeHomeBean getData = ResponseUtils.getTreeHomeDatas(
								mActivity, result);
						if (getData != null) {
							oldTreeLeaf = Integer.parseInt(getData.peibancount);
						}
					}
				}
			});
		} else {
			ShowToast.showShort(mActivity, R.string.net_break);// 提示无网络
		}
	}

	@Override
	public void onHiddenChanged(boolean hidden) {
		// TODO Auto-generated method stub
		super.onHiddenChanged(hidden);
		if(!hidden){
			if(viewPager!=null){
				switch (viewPager.getCurrentItem()) {
				case 0:
					if(grow!=null){
						grow.loadingGrowTreeDatas();
						MenuActivity parentAct = (MenuActivity)getActivity();
						if(parentAct!=null){
							parentAct.dismissBackgroundImg();
						}
					}
					
					break;
				case 1:
					break;
				default:
					break;
				}
			}
			
		}
	}
}
